export default function (refValue) {
  return {
    mounted() {
      // 绑定上下文
      this.handleMainScroll = this.handleMainScroll.bind(this);
      this.handleSetMainScroll = this.handleSetMainScroll.bind(this);

      this.$bus.$on("setMainScroll", this.handleSetMainScroll);
      this.$refs[refValue].addEventListener("scroll", this.handleMainScroll);
    },
    beforeDestroy() {
      // 先移除监听器
      this.$refs[refValue].removeEventListener("scroll", this.handleMainScroll);
      this.$bus.$off("setMainScroll", this.handleSetMainScroll);
    },
    methods: {
      handleMainScroll() {
        this.$bus.$emit("mainScroll", this.$refs[refValue]);
      },
      handleSetMainScroll(scrollTop) {
        // 确保 DOM 存在
        if (this.$refs[refValue]) {
          this.$refs[refValue].scrollTop = scrollTop;
        }
      },
    },
  };
}
